package com.lanhe.banner.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanhe.banner.domian.LhBanner;
import com.lanhe.banner.mapper.LhBannerMapper;
import com.lanhe.banner.service.ILhBannerService;
import com.lanhe.common.constant.Constants;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * banner图管理(LhBanner)表服务实现类
 *
 * @author zhanggl
 * @since 2023-12-30 13:58:58
 */
@Service
public class LhBannerServiceImpl extends ServiceImpl<LhBannerMapper, LhBanner> implements ILhBannerService {

    @Resource
    private LhBannerMapper bannerMapper;

    @Override
    public List<LhBanner> selectBannerList(LhBanner banner) {
        LambdaQueryWrapper<LhBanner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotNull(banner.getType()), LhBanner::getType, banner.getType());
        queryWrapper.ge(ObjectUtil.isNotNull(banner.getBeginTime()), LhBanner::getCreateTime, banner.getBeginTime());
        queryWrapper.le(ObjectUtil.isNotNull(banner.getEndTime()), LhBanner::getCreateTime, banner.getEndTime());
        queryWrapper.eq(LhBanner::getDelFlag, 0);
        queryWrapper.orderByDesc(LhBanner::getSort);
        return bannerMapper.selectList(queryWrapper);
    }

    @Override
    public int insertBanner(LhBanner banner) {
        return  bannerMapper.insert(banner);
    }

    @Override
    public LhBanner selectBannerById(Long bannerId) {
        LambdaQueryWrapper<LhBanner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LhBanner::getDelFlag, 0)
                .eq(LhBanner::getBannerId, bannerId);
        return  bannerMapper.selectOne(queryWrapper);
    }

    @Override
    public int updateBanner(LhBanner banner) {
        LambdaQueryWrapper<LhBanner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LhBanner::getDelFlag, 0)
                .eq(LhBanner::getBannerId, banner.getBannerId());
        return bannerMapper.update(banner, queryWrapper);
    }

    @Override
    public int deleteBannerById(Long bannerId) {
        LambdaUpdateWrapper<LhBanner> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(LhBanner::getDelFlag, Constants.GeneralIntType.TWO+"")
                .in(LhBanner::getBannerId,bannerId);
        return bannerMapper.update(null,updateWrapper);
    }
}

